# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Contributor: Nicolas Lorin <androw95220@gmail.com>
# Contributor: John Regan <john@jrjrtech.com>
# Maintainer: rubicon <rubicon@mailo.com>
pkgname=ejabberd
pkgver=22.05
pkgrel=1
_base64url=1.0.1
_cache_tab=1.0.30
_eimp=1.0.22
_ejabberd_po=9849703c3664e43452eae3245016a1c187f3edc4
_elixir=1.12.3
_epam=1.0.12
_eredis=1.2.0
_esip=1.0.47
_ezlib=1.0.12
_fast_tls=1.1.15
_fast_xml=1.1.49
_fast_yaml=1.0.33
_goldrush=0.2.0
_idna=6.0.0
_jiffy=1.1.1
_jose=1.11.1
_lager=3.9.2
_luerl=1.0
_mqtree=1.0.15
_p1_acme=1.0.19
_p1_mysql=1.0.19
_p1_oauth2=0.6.11
_p1_pgsql=1.1.18
_p1_utils=1.0.25
_pkix=1.0.9
_rebar_elixir_plugin=0.1.0
_sqlite3=1.1.13
_stringprep=1.0.28
_stun=1.2.2
_unicode_util_compat=0.4.1
_xmpp=1.5.8
_yconf=1.0.13
pkgdesc="An erlang jabber server"
url="https://www.ejabberd.im/"
arch="all"
license="GPL-2.0-or-later"
depends="erlang"
makedepends="automake autoconf gd-dev erlang-dev openssl-dev zlib-dev sqlite-dev yaml-dev linux-pam-dev expat-dev heimdal-dev"
pkgusers="ejabberd"
pkggroups="ejabberd"
install="$pkgname.pre-install"
options="!check" # test suite requires all kinds of services running
subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc $pkgname-bash-completion"
source="$pkgname-$pkgver.tar.gz::https://github.com/processone/ejabberd/archive/$pkgver.tar.gz
	base64url-$_base64url.tar.gz::https://github.com/dvv/base64url/archive/refs/tags/$_base64url.tar.gz
	cache_tab-$_cache_tab.tar.gz::https://github.com/processone/cache_tab/archive/refs/tags/$_cache_tab.tar.gz
	eimp-$_eimp.tar.gz::https://github.com/processone/eimp/archive/refs/tags/$_eimp.tar.gz
	ejabberd_po-$_ejabberd_po.tar.gz::https://github.com/processone/ejabberd-po/archive/$_ejabberd_po.tar.gz
	elixir-$_elixir.tar.gz::https://github.com/elixir-lang/elixir/archive/refs/tags/v$_elixir.tar.gz
	elixir.app.src elixir-mix.sh elixir-rebar.config
	epam-$_epam.tar.gz::https://github.com/processone/epam/archive/refs/tags/$_epam.tar.gz
	eredis-$_eredis.tar.gz::https://github.com/wooga/eredis/archive/refs/tags/v$_eredis.tar.gz
	esip-$_esip.tar.gz::https://github.com/processone/esip/archive/refs/tags/$_esip.tar.gz
	ezlib-$_ezlib.tar.gz::https://github.com/processone/ezlib/archive/refs/tags/$_ezlib.tar.gz
	fast_tls-$_fast_tls.tar.gz::https://github.com/processone/fast_tls/archive/refs/tags/$_fast_tls.tar.gz
	fast_xml-$_fast_xml.tar.gz::https://github.com/processone/fast_xml/archive/refs/tags/$_fast_xml.tar.gz
	fast_yaml-$_fast_yaml.tar.gz::https://github.com/processone/fast_yaml/archive/refs/tags/$_fast_yaml.tar.gz
	goldrush-$_goldrush.tar.gz::https://github.com/DeadZen/goldrush/archive/refs/tags/$_goldrush.tar.gz
	idna-$_idna.tar.gz::https://github.com/benoitc/erlang-idna/archive/refs/tags/$_idna.tar.gz
	jiffy-$_jiffy.tar.gz::https://github.com/davisp/jiffy/archive/refs/tags/$_jiffy.tar.gz
	jose-$_jose.tar.gz::https://github.com/potatosalad/erlang-jose/archive/refs/tags/$_jose.tar.gz
	lager-$_lager.tar.gz::https://github.com/erlang-lager/lager/archive/refs/tags/$_lager.tar.gz
	luerl-$_luerl.tar.gz::https://github.com/rvirding/luerl/archive/refs/tags/v$_luerl.tar.gz
	mqtree-$_mqtree.tar.gz::https://github.com/processone/mqtree/archive/refs/tags/$_mqtree.tar.gz
	p1_acme-$_p1_acme.tar.gz::https://github.com/processone/p1_acme/archive/refs/tags/$_p1_acme.tar.gz
	p1_mysql-$_p1_mysql.tar.gz::https://github.com/processone/p1_mysql/archive/refs/tags/$_p1_mysql.tar.gz
	p1_oauth2-$_p1_oauth2.tar.gz::https://github.com/processone/p1_oauth2/archive/refs/tags/$_p1_oauth2.tar.gz
	p1_pgsql-$_p1_pgsql.tar.gz::https://github.com/processone/p1_pgsql/archive/refs/tags/$_p1_pgsql.tar.gz
	p1_utils-$_p1_utils.tar.gz::https://github.com/processone/p1_utils/archive/refs/tags/$_p1_utils.tar.gz
	pkix-$_pkix.tar.gz::https://github.com/processone/pkix/archive/refs/tags/$_pkix.tar.gz
	rebar_elixir_plugin-$_rebar_elixir_plugin.tar.gz::https://github.com/processone/rebar_elixir_plugin/archive/refs/tags/$_rebar_elixir_plugin.tar.gz
	sqlite3-$_sqlite3.tar.gz::https://github.com/processone/erlang-sqlite3/archive/refs/tags/$_sqlite3.tar.gz
	stringprep-$_stringprep.tar.gz::https://github.com/processone/stringprep/archive/refs/tags/$_stringprep.tar.gz
	stun-$_stun.tar.gz::https://github.com/processone/stun/archive/refs/tags/$_stun.tar.gz
	unicode_util_compat-$_unicode_util_compat.tar.gz::https://github.com/benoitc/unicode_util_compat/archive/refs/tags/$_unicode_util_compat.tar.gz
	xmpp-$_xmpp.tar.gz::https://github.com/processone/xmpp/archive/refs/tags/$_xmpp.tar.gz
	yconf-$_yconf.tar.gz::https://github.com/processone/yconf/archive/refs/tags/$_yconf.tar.gz

	ejabberd.initd
	ejabberd.logrotate
	ejabberd.confd

	dont-leak-destdir.patch
	fix-elixir-dirs.patch
	fix-missing-plugins.patch
	"

prepare() {
	mkdir deps
	for i in \
	base64url-$_base64url \
	cache_tab-$_cache_tab \
	eimp-$_eimp \
	elixir-$_elixir \
	epam-$_epam \
	eredis-$_eredis \
	esip-$_esip \
	ezlib-$_ezlib \
	fast_tls-$_fast_tls \
	fast_xml-$_fast_xml \
	fast_yaml-$_fast_yaml \
	goldrush-$_goldrush \
	jiffy-$_jiffy \
	lager-$_lager \
	luerl-$_luerl \
	mqtree-$_mqtree \
	p1_acme-$_p1_acme \
	p1_mysql-$_p1_mysql \
	p1_oauth2-$_p1_oauth2 \
	p1_pgsql-$_p1_pgsql \
	p1_utils-$_p1_utils \
	pkix-$_pkix \
	rebar_elixir_plugin-$_rebar_elixir_plugin \
	stringprep-$_stringprep \
	stun-$_stun \
	unicode_util_compat-$_unicode_util_compat \
	xmpp-$_xmpp \
	yconf-$_yconf; do
		mv "$srcdir"/"$i" "$builddir"/deps/${i%-*}
	done
	mv "$srcdir"/ejabberd-po-$_ejabberd_po "$builddir"/deps/ejabberd_po
	mv "$srcdir"/erlang-sqlite3-$_sqlite3 "$builddir"/deps/sqlite3
	mv "$srcdir"/erlang-idna-$_idna "$builddir"/deps/idna
	mv "$srcdir"/erlang-jose-$_jose  "$builddir"/deps/jose

	install -Dm755 "$srcdir"/elixir-mix.sh \
		"$builddir"/deps/elixir/bin/mix
	install -Dm644 "$srcdir"/elixir-rebar.config \
		"$builddir"/deps/elixir/rebar.config
	install -Dm644 "$srcdir"/elixir.app.src \
		-t "$builddir"/deps/elixir/src/
	sed -i "s/@ELIXIR_VERSION@/$_elixir/" \
		"$builddir"/deps/elixir/src/elixir.app.src
	cp -a "$builddir"/deps/elixir/bin/elixir \
		"$builddir"/deps/elixir/bin/elixir-install

	default_prepare
	sed -i "s/@ELIXIR_VERSION@/$_elixir/" Makefile.in
	GIT_DIR=. sh ./autogen.sh
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--sbindir=/usr/sbin \
		--libdir=/usr/lib/ejabberd \
		--localstatedir=/var \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--enable-user=ejabberd \
		--enable-group=ejabberd \
		--enable-all
	make
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm755 tools/captcha* \
		-t "$pkgdir"/usr/share/doc/$pkgname/examples
	install -Dm644 tools/ejabberdctl.bc \
		"$pkgdir"/usr/share/bash-completion/completions/ejabberdctl

	install -d "$pkgdir"/var/spool/$pkgname
	install -d "$pkgdir"/var/lib/$pkgname
	install -D -m0644 "$srcdir"/$pkgname.logrotate \
		"$pkgdir"/etc/logrotate.d/$pkgname
	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	chown -R ejabberd.ejabberd "$pkgdir"/var/log/ejabberd
	chown -R ejabberd.ejabberd "$pkgdir"/var/spool/ejabberd
	chown -R ejabberd.ejabberd "$pkgdir"/var/lib/ejabberd
	chgrp ejabberd "$pkgdir"/etc/ejabberd/ejabberd.yml \
		"$pkgdir"/etc/ejabberd/ejabberdctl.cfg "$pkgdir"/etc/ejabberd

	sed -i "s|/usr/bin/iex|/usr/lib/ejabberd/ejabberd-$pkgver/priv/bin/iex|" \
		"$pkgdir"/usr/sbin/ejabberdctl

	cd "$pkgdir"/usr/lib/ejabberd/ejabberd-$pkgver/priv/bin/
	rm -f captcha.sh
	ln -sf ../../../*/priv/bin/* .
}

sha512sums="
33f98a9c45ff8257d240e7b90181e399ecab84e361f5619dc8d57252642e81d4958467cfa89b90681f3cf0d1d56a3079eb98d4e80cc6af5ca11938bb2a69f865  ejabberd-22.05.tar.gz
7398dc99078053b6e0544eb8ca93e9616549493c02245084c220ec854f429139dcaba38dd522858dd74b95067979a3ec4378eb3abd5f6d1662af27687381c738  base64url-1.0.1.tar.gz
bc8aaacea404d8a8b9a8fb14bc1eec9ec0c07ba4fac910287f448be66b0b3475b3913297dfce8d9de9fc208d9eb413dd8bf154cd1bd6edf86ee40100629e4553  cache_tab-1.0.30.tar.gz
5426df9a00d5b614ebb236729e2b397d5e72e12e68cbed32550a074b1b86557e1dee6585b917166e5ebf2fc41297329574c181471885638236c82a66a29d2232  eimp-1.0.22.tar.gz
ee70d9fcd28b5b1b43206ead9283b848f1d526a242b323ccc4e1211759683d914ad8ce9e33d4b864490b1a6e3f0542956eb1fa4fc8e1502a0a36587f41c3aab9  ejabberd_po-9849703c3664e43452eae3245016a1c187f3edc4.tar.gz
945e33ca9c3e50015531f87630c1bc1b9966acc64bbf7d01c8c4f8cfc104ee1e08113f572f0ad6a9f5e8ee37e33144e58b2ac33dc0a2b01e1355fc5f8b1b9329  elixir-1.12.3.tar.gz
ffb1ed8db363f008bc08af2871d6defa89f197347e488c739218c80143e217c71a7fd7579ba4949ce21f15027221038a78fc33803264502be8fa0b0a475ccf13  elixir.app.src
29ac3d68b3a69c720ed94194c24702cb1ceb2cafd47ad1dd52a7a74a29557fe06a31d671612c4e4d03c60b15be7208d0d993fec22cc1334dcc04ab6c34a79b58  elixir-mix.sh
4b10f66bdfe044b71849499127c68891185b1d3ec05efae5b5f9c7969cd364deea0d7635a9c918a0791ce84001470c267c087a41c647bed67c9aae1648e6d447  elixir-rebar.config
61a1d49c3316fb7f8fcc8b7d7c1ab0b5dd87fa31cecac679a850d191fe9171f7e01473b9979f8107c1c4567836e07ced67336dd5133dfdc5d4680cb3b6c3b270  epam-1.0.12.tar.gz
c72ae291389e47d1ac4c1de69dccb6121f4f9195283665d490904226b2ea15cb7671b48bb08db507a5e62c6bced62d890a9ecb9752da6e0aad24a28da137907c  eredis-1.2.0.tar.gz
68be1c8cf507180e73f3a16dfd8ca44ffad825c40df0d1874888afa41f3cb89b445b033036d041af29ec3fdbdab7acc68f770124171ba4af73abd0775a03c0ba  esip-1.0.47.tar.gz
950a76ef6714882909c8c03cb0c21f246fd04bb42b44496b8865a96da54544fad17b2e3f54ef0b744ae32a1b9930f32c5fbcd526833f3736e8e5087483e61233  ezlib-1.0.12.tar.gz
fcbdb9ffbb61c07fb3706bdae9d6d10f425968e722bcd03e2c86a99ca62d3ff0cb1298bf625adb679c627f38953fd3ef0b4263ae34ea100e9134488d9c55e192  fast_tls-1.1.15.tar.gz
c33e94865b676f0011f21f9434e0ac3c019abdad7cf0b229a4dcad8bf6c26d4d84585e013cc8257cbb4da4a4f4d5216702dbf2eda15bd333fd429d260e6e85f5  fast_xml-1.1.49.tar.gz
ace64e8818604031180955c2527cbd4eb73246f60d9d23e2fcde54d25451c3e2b0625e97df817a48cb23593f10555063aa894dd84fa64e5a2d68884a8d75a4f5  fast_yaml-1.0.33.tar.gz
1a74f02c75e72f8a60366b9ba74b763eb98621da2ce6044ed97e836566caf8ce85fadeee62b3d24303df319e2c1333d1c544cd267a041b718b0ab36de53e7b1a  goldrush-0.2.0.tar.gz
59f8996199f7e3391b1da1b8fb1ebb70e442838eab1f26af9a8a8545f3e1b9f72f0afd65108272a51d846b34e0e8abe9623f10c89156ebca3d87b6b3eb63a066  idna-6.0.0.tar.gz
cfa36d77285e53ccce852640a4c780f2c29ad33c7352e3a5b69fec431d6684bac4bf5ac323379a8bb1d1289afa23b6f73d4d5ecf1b025791fd84198d4c066910  jiffy-1.1.1.tar.gz
eed87ba597b8df064f38b873216f0a54fc4173d4393de49b648bc9e4e519778c4f3043701f8e6cb84a0f452ff555fcfa1d65afeb91d4466616bcc27838772fd6  jose-1.11.1.tar.gz
13b08145ab2316215f95de34838445f2922513e44f6800f338cd6941e6854653a9f8657447b439d5d508cb4569fbf4a0e03dc508d7c5d2ad48c9d4d14e17ca50  lager-3.9.2.tar.gz
b599f3562faeb672bef531642c0073c52943775e6785b3f8800559c1f6a331ec523987f4a3b1882da3e2d94b6257c5bc0992acdb5b3f250353bb8f25e081fa2e  luerl-1.0.tar.gz
e50066c39bb7feb4bea6a0fcff65ec4e73fca3aeeafa85be10ef7fe354e582a037369dc27163e47796f021e8cb70c40c396e5edce0ebf8255d4e452c4df0d4da  mqtree-1.0.15.tar.gz
d0896ca53fd48798f0e3f502a09e9df6ea88e4248968b5273b1a6e0f6735c39d680de251e0f2c49d04271018a5664252771be1746cf9d18ddd9d42e2ddf90b24  p1_acme-1.0.19.tar.gz
eee2402661cc7d58228641057c571c383c09261a0022287ca19ab56a51530ea575478971fea944919aeb910e37cd9dedfea629bdd949b8dae7892aa5590ac675  p1_mysql-1.0.19.tar.gz
c97fe256ba3d0c3c18d0ad14dd85f28bed7607b0aa8d56a23e1c87694a10669eb1cd6e0abafff7e366974b2160ef5eb8e65dadc60a85963e3aa4334252db8b6e  p1_oauth2-0.6.11.tar.gz
b075475a8729ca4d24d8cf05dc82b17a5c1e05c38d22586fe490c39194640e36145b0732126bb023881d6b258411a87046ab81a25fd85a1d3505d28da0d50122  p1_pgsql-1.1.18.tar.gz
e96bcd309dfaf3f80de573b7ad1b7281ee0cb97b0b91f92a5a9c5b5ccc969dce4d6b1adbaef9ff33bf9deb0da8e6b1738fbe3f69235b51a0069e0fda72a16d74  p1_utils-1.0.25.tar.gz
bd428814421effdc74c418a376b44716f4f79095550dd1b8431bfb51e8d26262015be0dda7948a9b8f9fc60a0cd0d12289161c8b497134f6413fd1e1efb886ba  pkix-1.0.9.tar.gz
6c9704e999d375fafdb08a38bf0850c487e8408e60e7ec355c4b4d9fe385335a691b09370029cf619ba9eef4d82383e9410ac7f18a730cc80bc3366e86644609  rebar_elixir_plugin-0.1.0.tar.gz
9e9e254511c0da74367e74c5b42cbe1bcc42a8232c1d60549b5a71f4db072391d7870b069ce247a972bef05f16d70b22b4c37176a59e44e75dc975946c86581f  sqlite3-1.1.13.tar.gz
1bedef40b38494ff42f551fedb83e47a2938eb29871b8649a76521035f16a7a2575d6524e12dfdfc45379c3cbe50af2988daa9ae9ab82632156514c86e06df7e  stringprep-1.0.28.tar.gz
ddbb13300c1cc2ac09d7049fbd97fcd67e7f2e0dbb56569279ea1b1e4fe5e106712178e683757a4c205ddc54d1b2a0291369affc42a23325d7ad6ad61b648fd9  stun-1.2.2.tar.gz
ee5bc1600ea00ad9c1d9f1565d04d073dd357c6f69c0728e770e17f7e4f1a07c2d2883d05e344d33dd7fe62e310ae4b86e4a05998e4830fedc6b895653993532  unicode_util_compat-0.4.1.tar.gz
0ef76dd65741072276ef3590e4c82456c832dda530f857a30072acc6444532865b716c82e60e972320ddab191ac1fb0a72be2bc71b01a72e16892015381086e3  xmpp-1.5.8.tar.gz
1e5df69421a82e8c442e17ed74738a4717e84521570f48a885de3e4fda2cbbc6d7567736d87255050d21e85edf976532ba6659b8a51d3150d1ac5e2c19d89a61  yconf-1.0.13.tar.gz
18fb890794be6b0e63960af80a39a97b8c70a6c0ab9a91a95d0f623257e38f5a43c012430ccf871038fc440d06720c573821b485a94191bd73e3924e670683c5  ejabberd.initd
47fd2cfd9177c4e978a9799a153ba74392a9891822221af8194686a40f6bf01f38644833e1e1f5416c6357e0bfb7ca3dae96f55a4fcd7cd629ec798d85a72807  ejabberd.logrotate
96a571c0ab2be366e931bda423a61ef920cbaba2107e61ddbc501472ce3efe2804418cc6579c99310b902a9a99aaecb9284cf2420c071dbca2f670efb4034135  ejabberd.confd
7b6debff487880dfacd8e882c03a9d948178dfd450c336657e11442a51b62574e9bd41c0af131b6b4f3b1b0f450c442c2eff04ad3f13439540685740295bf71e  dont-leak-destdir.patch
e1efee3ef1535febfe9a9d10b1ba1ecf798d1b17d14accc43b2c77536b93a956ae6c4c71c135f7e6d03bf2cd4f61bb0c12d27ef69576db71c3a915dc7c61c103  fix-elixir-dirs.patch
b6e23b16a8f931a2da9f052333dedc2b37667cf9265b84174590ac9a72e5f16d9dcd4c7153532bf45def7f94a71ce012dbb9d3227b7c7449b105fd711d085d59  fix-missing-plugins.patch
"
